﻿using System;
using System.Collections;
using System.Collections.Generic;

using IBatisNet.DataAccess;

using AUV5.Model;
using AUV5.DAL;
using AUV5.DAL.Interface;
using AUV5.DAL.Dao;
using IBatisNet.DataMapper;
namespace AUV5.BLL
{
	/// <summary>
    /// 功能：     ExtraService业务逻辑实现
    /// 作者：     LI
    /// 日期：     12/11/2011
    /// 修改日期： 
    /// 修改人：
    /// 修改内容：
    /// </summary>
	public class ExtraServiceBLL:BaseBLL
	{
		private static ExtraServiceBLL instance = null;
		private static object syncRoot = new object();
        private IDaoManager daoManager = ServiceConfig.Instance.DaoManager;
        private IExtraServiceDao dao = null;

		private ExtraServiceBLL()
        {
            dao = ExtraServiceSqlMapDao.Instance;
        }
		
		 public static ExtraServiceBLL Instance
        {
            get 
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new ExtraServiceBLL();
                        }
                    }
                }
                return instance;
            }
        }
		#region	基本功能 不能修改
		
		
		/// <summary>
		/// 得到最大行数
		/// </summary>
		/// <returns></returns>
        public int GetMaxCounts()
        {
            try
            {
                //daoManager.BeginTransaction();

                int count = dao.GetMaxCounts();

                //daoManager.CommitTransaction();

                return count;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }
		
		/// <summary>
		/// 得到列表
		/// </summary>
		/// <returns></returns>
        public System.Collections.Generic.IList<ExtraService> GetList()
        {
            try
            {
                //daoManager.BeginTransaction();

                System.Collections.Generic.IList<ExtraService> list = dao.GetList();

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }
		
		/// <summary>
		/// 通过不同参数得到列表
		/// </summary>
		/// <param name="TopCounts"></param>
		/// <returns></returns>
        private System.Collections.Generic.IList<ExtraService> GetListByParameters(object TopCounts, ExtraService extraservice)
        {
            try
            {
                //daoManager.BeginTransaction();
				
				Hashtable ht = new Hashtable();
				ht.Add("TopCounts",TopCounts);
                System.Collections.Generic.IList<ExtraService> list = dao.GetListByParameters(ht);

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }
		
		
		/// <summary>
		/// 得到明细
		/// </summary>
		/// <param name="ExtraID"></param>
		/// <returns></returns>
		public ExtraService GetDetail(System.String extraid)
		{
			try
            {
                ///daoManager.BeginTransaction();

                return dao.GetDetail(extraid);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
		}
		
		
		/// <summary>
        /// 新建
        /// </summary>
        /// <param name="extraservice"></param>
        /// <returns></returns>
		public object Add(ExtraService extraservice)
		{
			//int id = GetId("SYS_UserInfoTemp");
			//sys_userinfotemp.UserID = id;
			
			try
            {
                ///daoManager.BeginTransaction();

                return dao.Add(extraservice);
                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
		}
		
		/// <summary>
        /// 修改
        /// </summary>
        /// <param name="extraservice"></param>
        /// <returns></returns>
		public int Update(ExtraService extraservice)
		{
			try
            {
                ///daoManager.BeginTransaction();

                return dao.Update(extraservice);
		
				///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
		}

		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="ExtraID"></param>
		/// <returns></returns>
		public int Delete(System.String extraid)
		{
			try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(extraid);
	
				///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
		}
		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="extraservice"></param>
		/// <returns></returns>
		public int Delete(ExtraService extraservice)
		{
			try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(extraservice);
	
				///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
		}
		
		#endregion
		
		#region 扩展功能
		
		#endregion

        /// <summary>
        /// 添加增值服务
        /// </summary>
        public bool AddExtraService(ExtraService service)
        {
            try
            {
                if (!isExists(service))
                {
                    service.ExtraID = Guid.NewGuid().ToString();
                    service.State = (int)Common.Enum.auvState.Enabled;
                    service.IsDel = (int)Common.Enum.auvDel.NotDel;
                    service.CreateDate = DateTime.Now;
                    service.UpdateDate= DateTime.Now;
                    service.BeginDate = DateTime.Now;
                    service.EndDate = DateTime.Now;

                    Add(service);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        /// <summary>
        /// 判断一条服务是否已经存在（服务ID和标题）
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        private bool isExists(ExtraService service)
        {
            try
            {
                return dao.isExists(service);
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        /// <summary>
        /// 获取某服务的所有可用增值服务
        /// </summary>
        /// <param name="serviceid"></param>
        public IList<ExtraService> GetDetailByServiceID(string serviceid)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("serviceID", serviceid);
                ht.Add("state", (int)Common.Enum.auvState.Enabled);
                ht.Add("isDel", (int)Common.Enum.auvDel.NotDel);

                return dao.GetListByParameters(ht);

            }
            catch (Exception)
            {
                
                throw;
            }
        }


        public void Test()
        {
            string newid = Guid.NewGuid().ToString();
            try
            {
                
                SqlMap.BeginTransaction();

                ExtraService service = new ExtraService();
                service.ExtraID = newid;
                service.State = (int)Common.Enum.auvState.Enabled;
                service.IsDel = (int)Common.Enum.auvDel.NotDel;
                service.CreateDate = DateTime.Now;
                service.UpdateDate = DateTime.Now;
                service.BeginDate = DateTime.Now;
                service.EndDate = DateTime.Now;
                service.ServiceID = "test";
                service.Title = "test";
                service.Price = 110;
                Add(service);

                ExtraService result = GetDetail(newid);

                int i = int.Parse("ss");
                SqlMap.CommitTransaction();
            }
            catch (Exception)
            {
                SqlMap.RollBackTransaction();
                ExtraService result = GetDetail(newid);
            }
        }
    }
}
